<template>
    <div>
        <el-row style="margin: 18px 0px 0px 18px ">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/admin/dashboard' }">管理中心</el-breadcrumb-item>
                <el-breadcrumb-item>内容管理</el-breadcrumb-item>
                <el-breadcrumb-item>图书管理</el-breadcrumb-item>
            </el-breadcrumb>
        </el-row>
        <edit-form @onSubmit="loadBooks()" ref="edit"></edit-form>
        <el-card style="margin: 18px 2%;width: 95%">
            <el-table
                    ref="multipleTable"
                    :data="books"
                    stripe
                    style="width: 100%"
                    :max-height="tableHeight"
                    @selection-change="opt">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column type="expand">
                    <template v-slot="props">
                        <el-form label-position="left" inline>
                            <el-form-item>
                                <span>{{ props.row.abs }}</span>
                            </el-form-item>
                        </el-form>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="书名（展开查看摘要）"
                        fit>
                </el-table-column>
                <el-table-column
                        prop="category.name"
                        label="分类"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="author"
                        label="作者"
                        fit>
                </el-table-column>
                <el-table-column
                        prop="date"
                        label="出版日期"
                        width="120">
                </el-table-column>
                <el-table-column
                        prop="press"
                        label="出版社"
                        fit>
                </el-table-column>
                <!--<el-table-column-->
                <!--prop="abs"-->
                <!--label="摘要"-->
                <!--show-overflow-tooltip-->
                <!--fit>-->
                <!--</el-table-column>-->
                <el-table-column
                        fixed="right"
                        label="操作"
                        width="120">
                    <template v-slot="scope">
<!--                  native.      -->
                        <el-button
                                @click.prevent="editBook(scope.row)"
                                type="text"
                                size="small">
                            编辑
                        </el-button>
<!--                                native.      -->
                        <el-button
                                @click.prevent="deleteBook(scope.row.id)"
                                type="text"
                                size="small">
                            移除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div style="margin: 20px 0 20px 0;float: left">
                <el-button @click="deselect" >取消选择</el-button>
                <el-button @click="deleteBooks">批量删除</el-button>
            </div>
        </el-card>
    </div>
</template>

<script>
    import EditForm from './EditForm'
    import {computed, getCurrentInstance, onMounted, reactive, toRefs} from "vue";

    export default {
        name: 'BookManagement',
        components: {EditForm},
        setup() {
            const data = reactive({
                books: []
            })
            onMounted(() => {
                loadBooks()
            })
            let {proxy} = getCurrentInstance()

            let tableHeight = computed(() => {
                return window.innerHeight - 320
            })

            function opt(books){
                data.optionBooks = books
            }

            function deleteBook(id) {
                proxy.$confirm('此操作将永久删除该书籍, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                        proxy.$axios
                            .post('/admin/content/books/delete', {id: id}).then(resp => {
                            if (resp && resp.data.code === 200) {
                                loadBooks()
                            }
                        })
                    }
                ).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            }
            function deleteBooks(){
                proxy.$confirm('删除后无法恢复，请确定','提示').then(()=>{
                    data.optionBooks.forEach(book => {
                        proxy.$axios
                            .post('/admin/content/books/delete', {id: book.id}).then(resp => {
                            if (resp && resp.data.code === 200) {
                                console.log('成功删除')
                            }
                        })
                    })
                    loadBooks()
                }).catch(()=>{
                    console.log('取消删除')
                })

            }

            function editBook(item) {
                proxy.$refs.edit.dialogFormVisible = true
                proxy.$refs.edit.form = {
                    id: item.id,
                    cover: item.cover,
                    title: item.title,
                    author: item.author,
                    date: item.date,
                    press: item.press,
                    abs: item.abs,
                    category: {
                        id: item.category.id.toString(),
                        name: item.category.name
                    }
                }
                // this.$refs.edit.category = {
                //   id: item.category.id.toString()
                // }
            }

            function loadBooks() {
                proxy.$axios.get('/books').then(resp => {
                    if (resp && resp.data.code === 200) {
                        data.books = resp.data.result
                    }
                })
            }

            function deselect(){
                proxy.$refs.multipleTable.clearSelection()
            }


            return {
                ...toRefs(data),
                onMounted,
                loadBooks,
                editBook,
                deleteBook,
                tableHeight,
                deselect,
                opt,
                deleteBooks
            }
        }
    }
</script>

<style scoped>
</style>
